package com.mj;

import com.mj.entity.Student;
import com.mj.mapper.StudentMapper;
import com.mj.mybatis.MyHelper;
import org.junit.Test;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Demo {
    @Test
    public void ss() {
        MyHelper<StudentMapper> mh = new MyHelper<StudentMapper>();
        StudentMapper sm = mh.getMapper(StudentMapper.class);
        List<Student> li = sm.queryTotal();
        for (Student sd : li) {
            System.out.println(sd.getName());
        }
    }

    //使用存储过程
    @Test
    public void pro() {
        MyHelper mh = new MyHelper();
        Connection conn = mh.getConn();
        try {
            CallableStatement cs = conn.prepareCall("{Call stud}");
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void stu() {
        MyHelper<StudentMapper> mh = new MyHelper<StudentMapper>();
        StudentMapper st = mh.getMapper(StudentMapper.class);
        //st.del(22);
        st.update(32, "九叔");
        List<Student> li = st.stu();
        System.out.println(li.size());
        for (Student s : li) {
            System.out.println(s.getName());
        }
        mh.close();
    }

    @Test  //分页
    public void ppp() {
        MyHelper<StudentMapper> mh = new MyHelper<StudentMapper>();
        StudentMapper st = mh.getMapper(StudentMapper.class);
        int recordcount = st.getCount();
        int currentpage = 2;
        int size = 3;
        int pagecount = recordcount % size == 0 ? recordcount / size : recordcount / size + 1;
        if (currentpage < 1) currentpage = 1;
        if (currentpage > pagecount) currentpage = pagecount;
        System.out.printf("当前第%d页,共%d页,每页%d条,共%d条", currentpage, pagecount, size, recordcount);
        int start = currentpage * size - size;
        List<Student> ls = st.ppp(start, size);
        System.out.println(ls.size());
        for (Student sz : ls) {
            System.out.println(sz.getName());
        }
    }
}
